<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\VehicleModelRepository;

class VehicleModelService extends BaseService
{

    /**
     * 车型列表
     *
     * @param int $n_pid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_vehicle_model_list($n_pid = 0){
        $n_pid = intval($n_pid);
        $arr_option = [
          'where'=>['parentid'=>$n_pid],
          'field'=>['id','name','fullname','initial','parentid','logo','depth','salestate','price','yeartype','productionstate','sizetype'],
        ];
        $res = VehicleModelRepository::base_get_all($arr_option)->toarray();
        $res = collect($res)->sortBy('id')->sortBy('initial')->values()->all();
        return  hello_success('成功',$res);

    }

    /**
     * 车型列表
     *
     * @param int $n_pid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_vehicle_model_top_list($n_pid = 0){
        $n_pid = intval($n_pid);
        if($n_pid == 0){
            $arr_option = [
              'where'=>['parentid'=>$n_pid],
              'field'=>['id','name','fullname','initial','parentid','logo','depth'],
            ];
            $res = VehicleModelRepository::base_get_all($arr_option)->toarray();
            $res = collect($res)->where('id','!=',100000)->sortBy('id')->sortBy('initial')->groupBy('initial')->map(function ($value, $key) {
                $value = [
                  'title'=>$key,
                  'child'=>$value->toArray(),
                ];
                return $value;
            })->values()->toArray();
        }
        else{
            $arr_option = [
              'where'=>['parentid'=>$n_pid],
              'with'=>['child'],
              'field'=>['id','name','fullname','initial','parentid','logo','depth','salestate','price','yeartype','productionstate','sizetype'],
            ];
            $res = VehicleModelRepository::base_get_all($arr_option)->toarray();
            $res = collect($res)->sortBy('name')->map(function ($value, $key) {
                $value = [
                  'title'=>$value['name'],
                  'child'=>$value['child'],
                ];
                return $value;
            })->values()->toArray();

        }

        return  hello_success('成功',$res);

    }

}